package org.epidroid.epidroid.ratp.ui;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;


public class DataHandler extends DefaultHandler{
	private int position = 0;
	private int pos2 = -1;
	
	private boolean order1 = false;
	
	public DataHandler (Integer key) {
		super ();
		key_ = key;
	}
	
	public class Element {
		String texts[];
		
		Element () {
			texts = new String[3];
		}
	}
	
	public ArrayList<Element> elements = new ArrayList<Element>();
	public Integer key_;
	
	public void startElement(String namespaceURI, String localName,
			String qname, Attributes atts) {
		if (localName.contentEquals("schedule")) {
			int order = Integer.valueOf(atts.getValue("order"));
			order1 = (order == 1);
			return;
		}
		if (!order1) {
			return;
		}
		if (localName.contentEquals("item")) {
			position = Integer.valueOf(atts.getValue("order")) - 1;
			elements.add(new Element ());
		}
		if (localName.contentEquals("texte1"))
		{
			pos2 = 0;
			elements.get(position).texts[pos2] = "";
		}
		if (localName.contentEquals("texte2"))
		{
			pos2 = 1;
			elements.get(position).texts[pos2] = "";
		}
		if (localName.contentEquals("texte2"))
		{
			pos2 = 2;
			elements.get(position).texts[pos2] = "";
		}
	}
	
	public void characters (char[] ch, int start, int length) {
		if (pos2 != -1) {
			elements.get(position).texts[pos2] = elements.get(position).texts[pos2] + (new String(ch, start, length));
		}
	}

	public void endElement(String uri, String localName, String qName) {
		pos2 = -1;
		if (localName.contentEquals("order")) {
			order1 = false;
		}
	}
}
